package io.renren.modules.wx.service.impl;

import io.renren.modules.wx.entity.ChapterEntity;
import io.renren.modules.wx.entity.TeacherEntity;
import io.renren.modules.wx.service.TeacherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.wx.dao.CourseDao;
import io.renren.modules.wx.entity.CourseEntity;
import io.renren.modules.wx.service.CourseService;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Autowired
    TeacherService teacherService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                StringUtils.isBlank(key) ? new QueryWrapper<>() : new QueryWrapper<CourseEntity>().eq("arrtribute", key)
        );

        return new PageUtils(page);
    }

    @Override
    public List<CourseEntity> courseList() {
        List< CourseEntity> entities = baseMapper.selectList(null);
        List<CourseEntity> res = entities.stream().map((entity) -> {
            TeacherEntity teacher = teacherService.getById(entity.getTeacherId());
            entity.setTeacherName(teacher.getUsername());
            return entity;
        }).collect(Collectors.toList());
        return res;
    }

    @Override
    public List<CourseEntity> courseByCondition(Map<String, Object> params) {
        String key = (String)params.get("key");
        List<CourseEntity> entities = null;
        if (!StringUtils.isBlank(key)){
            entities = baseMapper.selectList(new QueryWrapper<CourseEntity>().eq("attribute",key));
        }else {
            entities  = baseMapper.selectList(new QueryWrapper<CourseEntity>());
        }
       List<CourseEntity> res =  entities.stream().map((entity) -> {
            TeacherEntity teachers = teacherService.getById(entity.getTeacherId());
            entity.setTeacherName(teachers.getUsername());
            return entity;
        }).collect(Collectors.toList());
        return res;
    }

}
